// pages/feedback/feedback.js
const {
  feedbackQuery,
  feedback
} = require('../../api/url.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShowTypeModal: false, //是否显示意见反馈modal
    user: {}, //用户信息
    temp: '', //用于选择反馈类型
    feedbackType: [], //反馈类型列表数据
    feedbackPhone: '', //反馈用户的手机号码
    type: '请选择反馈类型',
    contentWordCount: 0,
    contentLimit: 10,
    isIPX:false,//是否是iPhoneX
    contentLimit: 200,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      user: app.globalData.userInfo,
      isIPX:app.globalData.isIPX
    })
    this.loadfeedbackType()
  },
  inputContent(e) {
    let content = e.detail.value || ''
    if (content.length > this.data.contentLimit) {
      content = content.slice(0, 10)
    }
    this.setData({
      contentWordCount: content.length,
      content
    })
  },
  inputPhone(e) {
    this.setData({
      feedbackPhone: e.detail.value || ''
    })
  },
  clearPhone: function() {
    this.setData({
      feedbackPhone: ""
    })
  },
  checkPhone() {
    const {
      feedbackPhone
    } = this.data

    if (!feedbackPhone || feedbackPhone == '') {
      wx.showToast({
        title: '联系号码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else if (feedbackPhone && !/^((\+86)|(86))?1[34578][0-9]\d{8}$/.test(feedbackPhone)) {
      wx.showToast({
        title: '联系号码格式不对',
        icon: 'none',
        duration: 2000
      })
    }
  },
  //读取意见反馈类型
  loadfeedbackType: function() {
    var self = this
    const {
      user
    } = this.data
    wx.request({
      url: feedbackQuery,
      data: {
        page: 1,
        size: 100,
        token: wx.getStorageSync('token'),
        loginName: user.loginname,
      },
      success: function(res) {
        if (res.data.code == 0) {
          self.setData({
            feedbackType: res.data.response.cont.list
          })
        }
      }
    })
  },

  //切换显示反馈类型modal
  toggletype: function() {
    this.setData({
      isShowTypeModal: !this.data.isShowTypeModal
    })
  },

  //选中反馈类型
  selectType: function(e) {
    var feedbackType = e.currentTarget.dataset.feedbacktype
    var self = this
    self.setData({
      temp: feedbackType.fId,
      type: feedbackType.fName,
    })
    setTimeout(function() {
      self.setData({
        isShowTypeModal: false
      })
    }, 200)
  },
  //提交反馈
  tapSubmit() {
    const {
      temp,
      content,
      feedbackPhone,
      user
    } = this.data
    if (!temp) {
      wx.showToast({
        title: '请选择反馈类型',
        icon: 'none'
      })
      return
    }
    if (!content) {
      wx.showToast({
        title: '请填写反馈意见',
        icon: 'none'
      })
      return
    }
    if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(feedbackPhone || '')) {
      wx.showToast({
        title: '请填写有效手机号',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '正在提交',
    })
    wx.request({
      url: feedback,
      data: {
        token: wx.getStorageSync("token"),
        loginName: user.loginname,
        fFeedBackTypeId: temp,
        fContent: content,
        fPhone: feedbackPhone
      },
      success: function(res) {
        wx.hideLoading()
        if (res.data.code == 403) {} else if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: "反馈成功",
            icon: 'success',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack()
          }, 2000)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})